package cc.ttiot.thread.dataCollectionThread;

import cc.ttiot.service.dataCollection.serial.SerialCollection;
import cc.ttiot.service.dataCollection.serial.impl.TemperatureAndVibrationCollection;
import com.serotonin.modbus4j.exception.ModbusInitException;

/**
 * @Description: 温振传感器线程，通过串口通信，ModbusRTU协议
 * @Author WangSuheng
 * @Date 2021/10/2 12:00
 * @Version 1.0
 */

/*
    1)由于该传感器暂时被弃用，因此mapper层的dao和domain还没写
    2)该类需要将根目录下的lib目录内的commons-logging和modbus4j进行add as library操作，否则会报找不到类的错误
 */
public class TemperatureAndVibrationCollectThread implements Runnable{

    public TemperatureAndVibrationCollectThread(){
        super();
    }

    @Override
    public void run() {
        SerialCollection collection = new TemperatureAndVibrationCollection();
        try {
            collection.init();
        } catch (ModbusInitException e) {
            e.printStackTrace();
        }
        System.out.println("串口初始化成功");

        //采集
        //该传感器从机编号为1，读地址为0x00-0x03的保持寄存器
        //00为温度 01-03为三轴振动
        //注意读出来的值都是实际值的十倍
        while (true) {
            short[] data = collection.collect();
            double temperature = ((double) data[0]) / 10;
            double vibration_x = ((double) data[1]) / 10;
            double vibration_y = ((double) data[2]) / 10;
            double vibration_z = ((double) data[3]) / 10;

            System.out.printf("温度: %.1f °C, x轴振动: %.1f mm/s, y轴振动: %.1f mm/s, z轴振动: %.1f mm/s\n",temperature,vibration_x,vibration_y,vibration_z);

        }

    }
}
